package net.dgg.rz.common.service;

import net.dgg.iboss.base.util.CustomerNumberEncryptionUtil;
import net.dgg.iboss.base.util.DesUtil;
import net.dgg.tmd.foundation.platform.common.util.TranslateUtil;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.rz.common.base.service.BaseService;
import net.dgg.rz.common.constant.ScProductConstant;
import net.dgg.rz.common.dao.ProductDao;
import net.dgg.rz.common.entity.OrderProductVo;
import net.dgg.rz.common.entity.Product;
import net.dgg.rz.common.utils.StringUtils;
import net.dgg.rz.common.utils.ValidateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 销售子订单
 */
@Service
public class ProductService extends BaseService {
    @Resource
    private ProductDao productDao;

    @Resource
    private CommonService commonService;
    @Resource
    private SessionManager sessionManager;
    @Resource
    private TranslateUtil translateUtil;
    @Resource
    private DesUtil desUtil;

    /**
     * 查询销售子订单信息（分页）
     *
     * @return list
     * @Param params
     */
    public List<OrderProductVo> findWaitAddCredentailProductWithPage(Map params) {
        if (null == params.get("orderColumn")) {
            params.put("orderDir", "desc");//排序方向
            params.put("orderColumn", "p.create_time");//排序的列
        }
        if (params.containsKey("isAll") && "0".equals(params.get("isAll"))) {
            params.put("signOrgId", commonService.findUserSyb(sessionManager.getCurrentUser()));
        }
        if (params.containsKey("contactPhone") && StringUtils.isNotEmpty(params.get("contactPhone"))) {
            params.put("contactPhone", desUtil.encrypt(params.get("contactPhone").toString()));
        }
        List<OrderProductVo> orderProducts = productDao.queryOrderProductVoWithPage(params);
        String[] column = {"costState", "payMethod"};
        orderProducts = translateUtil.translateList(column, orderProducts);
        String[] hideColum = {"customerPhone"};
        orderProducts = desUtil.decryptList(hideColum, orderProducts);
        orderProducts = CustomerNumberEncryptionUtil.encryptionList(hideColum, orderProducts);
        return orderProducts;
    }

    /**
     * 查询销售子订单信息
     *
     * @return list
     * @Param params
     */
    public List<OrderProductVo> findWaitAddCredentailProduct(Map params) {
        List<OrderProductVo> orderProducts = productDao.queryOrderProductVo(params);
        return orderProducts;
    }


    /**
     * 订单插入接口
     *
     * @param product
     * @return
     */
    public int iboosInsertProduct(Product product) {
        return productDao.iboosInsertSelective(product);
    }

    /**
     * 根据销售子订单id查询
     *
     * @param orderId
     * @return
     */
    public Product findProductByOrderId(Long orderId) {
        Map map = new HashMap();
        map.put("orderId", orderId);
        return productDao.findProductByOrderId(map);
    }

    /**
     * 根据销售子订单表的id查询
     *
     * @param id
     * @return
     */
    public Product selectByPrimaryKey(Long id) {
        return productDao.selectByPrimaryKey(id);
    }

    /**
     * 修改销售子订单
     *
     * @param product
     */
    @Transactional
    public void updateProduct(Product product) {
        productDao.updateByPrimaryKeySelective(product);
    }
}
